<template>
  <div class="app-container" v-permission="['system:user:query']">
    <el-row :gutter="5">
      <el-col :span="4">
        <el-card>
          <sys-user-left-tree @reload="reloadHandler"></sys-user-left-tree>
        </el-card>
      </el-col>
      <el-col :span="20">
        <el-card>
          <!--搜索表单开始-->
          <el-form :inline="true" size="small" :model="page.params">
            <el-form-item>
              <el-input v-model="page.params.userName" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="page.params.nickName" placeholder="请输入昵称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-select clearable v-model="page.params.status" placeholder="请选择状态">
                <el-option
                  v-for="item in statusOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="small" @click="search">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh" size="small" @click="resetSearch">重置</el-button>
            </el-form-item>
          </el-form>
          <!--搜索表单结束-->
          <!--用户数据表格开始-->
          <div>
            <el-button v-permission="['system:user:add']"
                       type="success" icon="el-icon-plus" style="margin-bottom: 5px" size="small"
                       @click="toAdd">添加
            </el-button>
            <el-table
              v-loading="loading"
              :data="dataPage.list"
              border
              stripe
              style="width: 100%"
            >
              <el-table-column prop="userName" label="账号" align="center"></el-table-column>
              <el-table-column prop="nickName" label="昵称" align="center"></el-table-column>
              <el-table-column prop="email" label="邮箱" align="center" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="phone" label="电话" align="center" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="leaderName" label="直属领导" align="center" width="80px"></el-table-column>
              <el-table-column prop="sex" label="性别" align="center" width="60px">
                <template slot-scope="{row}">
                  <dict-tag :data-list="sexOptions" :value="row.sex"></dict-tag>
                </template>
              </el-table-column>
              <el-table-column prop="deptName" label="所在部门" align="center"></el-table-column>
              <el-table-column prop="status" label="状态" align="center" width="60px">
                <template slot-scope="{row}">
                  <dict-tag :data-list="statusOptions" :value="row.status"></dict-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="{row}">
                  <el-dropdown>
                    <span class="el-dropdown-link">
                      <el-link icon="el-icon-s-operation" type="primary">操作<i
                        class="el-icon-arrow-down el-icon--right"></i></el-link>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item>
                        <el-button v-permission="['system:user:info']"
                                   type="text" icon="el-icon-document" @click="toInfo(row)">详情
                        </el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button v-permission="['system:user:update']"
                                   type="text" icon="el-icon-edit" @click="toUpdate(row)">修改
                        </el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button v-permission="['system:user:resetPwd']"
                                   type="text" icon="el-icon-refresh" @click="toResetPwd(row)">重置密码
                        </el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button v-permission="['system:user:remove']"
                                   type="text" icon="el-icon-delete" @click="toDelete(row)">删除
                        </el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button v-permission="['system:user:selectRole']"
                                   type="text" icon="el-icon-setting" @click="toSelectRole(row)">分配角色
                        </el-button>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page.pageNumber"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="page.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="dataPage.totalCount"
              align="center">
            </el-pagination>
          </div>
          <!--用户数据表格结束-->

        </el-card>
      </el-col>
    </el-row>
    <!--添加用户的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="addDialog"
               v-if="addDialog">
      <sys-user-add @after="getUserPage" @close="closeDialog"></sys-user-add>
    </el-dialog>
    <!--添加用户的弹出层结束-->

    <!--修改用户的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="updateDialog"
               v-if="updateDialog">
      <sys-user-update :active-id="activeId" @after="getUserPage" @close="closeDialog"></sys-user-update>
    </el-dialog>
    <!--修改用户的弹出层结束-->

    <!--用户详情的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="infoDialog"
               v-if="infoDialog">
      <sys-user-info :active-id="activeId" @close="closeDialog"></sys-user-info>
    </el-dialog>
    <!--用户详情的弹出层结束-->

    <!--用户分配角色的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="selectRoleDialog"
               v-if="selectRoleDialog">
      <sys-user-select-role :active-id="activeId"></sys-user-select-role>
    </el-dialog>
    <!--用户分配角色的弹出层结束-->

  </div>
</template>

<script>
import sysUserLeftTree from "./sys-user-left-tree"
import sysUserAdd from './sys-user-add'
import sysUserUpdate from './sys-user-update'
import sysUserInfo from './sys-user-info'
import sysUserSelectRole from './sys-user-select-role'
import sysUserApi from "@/api/system/sysUser";

export default {
  name: "sys-user-list.vue",
  components: {
    sysUserLeftTree,
    sysUserAdd,
    sysUserUpdate,
    sysUserInfo,
    sysUserSelectRole
  },
  data() {
    return {
      //加载的遮罩
      loading: false,
      //分页的参数
      page: {
        pageNumber: 1,
        pageSize: 10,
        params: {
          userName: undefined,
          nickName: undefined,
          status: undefined,
          deptId: undefined
        }
      },
      //用户的表格数据
      dataPage: {
        list: [],
        totalCount: 0
      },
      //状态字典数据
      statusOptions: [],
      //性别的字典数据
      sexOptions: [],
      //弹出层的标题
      title: "",
      //是否打开添加用户的弹出层
      addDialog: false,
      //是否打开修改用户的弹出层
      updateDialog: false,
      //当前选择行的ID
      activeId: undefined,
      //是否打开详情的弹出层
      infoDialog: false,
      //是否打开分配角色的弹出层
      selectRoleDialog: false
    }
  },
  created() {
    //加载状态的字典数据
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
    this.getDataByType("sys_user_sex").then(res => {
      this.sexOptions = res.data;
    })
    this.getUserPage()
  },
  methods: {
    //搜索
    search() {
      this.page.pageNumber = 1
      this.getUserPage();
    },
    // 重置搜索
    resetSearch() {
      this.page.params = {}
      this.getUserPage()
    },
    //每页条数发生变化时触发
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getUserPage()
    },
    //页码发生变化时触发
    handleCurrentChange(val) {
      this.page.pageNumber = val;
      this.getUserPage()
    },
    //加载所有的用户数据
    getUserPage() {
      this.loading = true;
      sysUserApi.getByPage(this.page).then(res => {
        this.dataPage.list = res.data.list
        this.dataPage.totalCount = res.data.totalCount
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    //打开添加用户的弹出层
    toAdd() {
      this.title = "添加用户"
      this.addDialog = true;
    },
    //打开用户详情的弹出层
    toInfo(row) {
      this.title = `【${row.nickName}】的详情`
      this.infoDialog = true;
      this.activeId = row.userId
    },
    //打开修改用户的弹出层
    toUpdate(row) {
      this.title = `修改【${row.nickName}】用户`
      this.updateDialog = true;
      this.activeId = row.userId
    },
    //重置用户密码
    toResetPwd(row) {
      this.$confirm(`此操作会重置【${row.nickName}】的用户密码, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysUserApi.resetPwd(row.userId).then(res => {
          this.$message.success('密码重置成功');
        })
      }).catch(() => {
        this.$message.info('已取消重置密码');
      });
    },
    //删除用户
    toDelete(row) {
      this.$confirm(`此操作会删除【${row.nickName}】该用户, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysUserApi.deleteById(row.userId).then(res => {
          this.$message.success('删除成功');
          this.getUserPage()
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    //打开用户分配角色的弹出层
    toSelectRole(row) {
      this.title = `给【${row.nickName}】分配角色`
      this.selectRoleDialog = true;
      this.activeId = row.userId
    },
    //关闭所有弹出层
    closeDialog() {
      this.addDialog = false;
      this.updateDialog = false;
      this.infoDialog = false
    },
    //左边的树点击之后传来的数据
    reloadHandler(deptId) {
      this.page.params.deptId = deptId
      this.getUserPage()
    },
  }
}
</script>

<style scoped>

</style>
